<?php
/**
 * 短信控制类
 * @author tangbl
 *
 */
class SmsAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}

	/**
	 * 发送短信
	 */ 
	public function sms_send(){
		
		if(IS_POST){
				
			$model = D("SmsSend");
			if($model->create()){
		
				$map = $model->data();
				$map['addid'] = session(C("USER_AUTH_KEY"));
				if(empty($map['table']) && empty($map['mobile'])){
					$this->error(L("sms_send_mobile_reuire"));
				}
				$json = sendRest("Sms/send",$map,"post");
				$this->success($json['info'],U("Sms/sms_send_list"));
				//dump($json);exit();
			}else{
				$this->error($model->getError());
			}
				
		}elseif(IS_AJAX){
			
			$model  = D("SmsTemp");
			$lang   = $this->_param("lang","",getLang());
			$tempid = $this->_param("tempid","intval",0);
				
			$content= $model->getTempContent($tempid,$lang);
			$this->ajaxReturn($content);
			
		}else{
			
			$this->display();
		}
	}
	
	/**
	 * 短信发送记录
	 */
	 public function sms_send_list(){
		
	 	if(IS_AJAX){
	 		 
	 		import('ORG.Util.Page');
	 		$model     = M("SmsSend");
	 		$orderDesc = $this->_get("orderDirection","trim","desc");
	 		$orderName = $this->_get("orderName","trim","addtime");
	 		$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
	 			
	 		$map       = unescape($this->_get("where"));
	 		$count     = $model->where($map)->count();
	 		$Page      = new Page($count);
	 		$nowPage   = $this->_get("p","intval",1);
	 		$list      = $model->where($map)->order($orderName.' '.$orderDesc)
	 					->page($nowPage.','.$pageNum)->select();
	 	
	 		$totalPage = ceil($count/$pageNum);
	 		C('web_admin_pagenum',$pageNum);
	 		$view      = L('eml_send_list_log');
	 		$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
	 		foreach($list AS $row){
	 			$entry = array('id'=>$row['id'],
	 				'cell'=>array(
	 					'id'      => $row['id'],
	 					'addtime' => date('Y-m-d H:i',$row['addtime']),
	 					'addid'   => $row['addid'],
	 					'send'    => $row['send'],
	 					'succ'    => $row['succ'],
	 					'fail'    => $row['fail'],
	 					'deal'    => '
 						 <a href="'.__URL__.'/sms_send_log/id/'.$row['solevar'].'" class="btn blue icn-only" title="'.$view.'"><i class="icon-list"></i></a>'
	 				),
	 			);
	 			$jsonData['rows'][] = $entry;
	 		}
	 		$this->ajaxReturn($jsonData,'JSON');
	 	}else{
	 		$this->display();
	 	}
	}
	
	/**
	 * 短信发送记录详情
	 */
	public function sms_send_log(){
		
		if(IS_AJAX){
		
			import('ORG.Util.Page');
			$model     = M("SmsSendLog");
			$orderDesc = $this->_get("orderDirection","trim","desc");
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
		
			$map       = unescape($this->_get("where"));
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$nowPage   = $this->_get("p","intval",1);
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
			->page($nowPage.','.$pageNum)->select();
		
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			$view      = L('eml_send_list_log');
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'      => $row['id'],
						'addtime' => date('Y-m-d H:i:s',$row['addtime']),
						'feed'    => $row['feed'],
						'status'  => $row['status'],
						'mobile'  => $row['mobile']
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
				
			$this->assign("solevar",$this->_get("id"));
			$this->display();
		}
	}
	
	
	/**
	 * 短信模板列表
	 */
	public function sms_temp(){
		 
		if(IS_AJAX){
	
			import('ORG.Util.Page');
			$model     = M("SmsTemp");
			$orderDesc = $this->_get("orderDirection","trim","desc");
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
		 	
			$map       = unescape($this->_get("where"));
			$map       = "isdel=2 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
					     ->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
	
			$del = L('cfg_list_itf_btn_delete');
			$upd = L('cfg_list_itf_btn_modify');
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'      => $row['id'],
						'sign'    => $row['sign'],
						'title'   => $row['title'],
						'addtime' => date('Y-m-d',$row['addtime']),
						'deal'    => '
 					<a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>
 					<a href="'.__URL__.'/sms_temp_edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
			$this->display();
		}
	}
	
	/**
	 * 添加短信模板
	 */
	public function sms_temp_add(){
		 
		$langs = C("LANG_ARRAY");
		 
		if(IS_POST){
	
			$value = $this->_post("value");
			$json  = json_encode($value);
	
			$param = array(
				"reply" => false,
				"url"   => array("url"=>"Sms/sms_temp"),
				"arr"   => array("content"=>$json)
			);
			parent::do_add("SmsTemp",$param);
		}else{
	
			$this->assign("langs",$langs);
			$this->display();
		}
	}
	
	/**
	 * 修改短信模板
	 */
	public function sms_temp_edit(){
		 
		if(IS_POST){
	
			$value = $this->_post("value");
			$json  = json_encode($value);
	
			$param = array(
					"reply" => false,
					"url"   => array("url"=>"Sms/sms_temp"),
					"arr"   => array("content"=>$json)
			);
			parent::do_edit("SmsTemp",$param);
		}else{
		 	
			$data   = D("SmsTemp");
			$id     = $this->_get("id","intval",0);
			$row    = $data->find($id);
		 	
			$value  = json_decode($row['content'],true);
			$langs  = C("LANG_ARRAY");
			foreach($langs as $k=>$v){
				$values[$k] = array("title"=>$v,"value"=>stripslashes($value[$k]));
			}
			 
			$this->assign("langs",$values);
			$this->assign("detail",$row);
			$this->display();
		}
	}
	
	/**
	 * 删除短信模板
	 * 物理删除
	 */
	public function sms_temp_del(){
		 
		parent::do_del("SmsTemp",false);
	}
	
}

?>